﻿using System;
using System.IO;
using System.Reflection;
using StockLib.UserData;

namespace StockLib
{
    public class StockUserData
    {
        #region 文件目录定义
        private const string DIR_DATA = @"userdata\";
        private const string DIR_BLOCK = @"block\";
        private const string DIR_XDR = @"xdr\";
        #endregion

        #region 文件后缀定义
        private const string EXT_XDR = @".xdr";
        #endregion

        #region 文件路径定义
        private const string FILE_DOMAIN = "domain.dat";
        private const string FILE_BLOCK = "block.dat";
        private const string FILE_TAB = "tabset.dat";
        #endregion

        /// <summary>
        /// 文件存储的根路径
        /// </summary>
        public string RootPath { get; set; }

        /// <summary>
        /// 单例实例
        /// </summary>
        private static StockUserData _instance = new StockUserData();

        /// <summary>
        /// 构造函数
        /// </summary>
        private StockUserData()
        {
            RootPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        }

        /// <summary>
        /// 访问单例
        /// </summary>
        /// <returns></returns>
        public static StockUserData Instance
        {
            get
            {
                return _instance;
            }
        }

        /// <summary>
        /// 初始化数据库
        /// </summary>
        public virtual void Init()
        {
            if (string.IsNullOrEmpty(RootPath))
                return;

            if (!Directory.Exists(RootPath))
                return;
        }

        /// <summary>
        /// 释放数据库
        /// </summary>
        public virtual void Release()
        {
        }

        /// <summary>
        /// 根据市场类型和数据类型，确定文件名称
        /// </summary>
        /// <param name="market">市场类型</param>
        /// <param name="dataType">数据类型</param>
        /// <returns></returns>
        public string GetFileName(UserDataType dataType)
        {
            string fileName = null;
            switch (dataType)
            {
                case UserDataType.Domain:
                    fileName = RootPath + DIR_DATA + FILE_DOMAIN;
                    break;

                case UserDataType.Block:
                    fileName = RootPath + DIR_DATA + FILE_BLOCK;
                    break;

                case UserDataType.Tab:
                    fileName = RootPath + DIR_DATA + FILE_TAB;
                    break;
            }

            return fileName;
        }

        public void LoadDomainData()
        {
            BlockRegularFileLoader domainLoader = null;
            if (string.IsNullOrEmpty(Properties.Settings.Default.BlockRegularLoaderClass))
            {
                domainLoader = new BlockRegularFileLoader();
            }
            else
            {
                domainLoader = Assembly.GetExecutingAssembly().CreateInstance(Properties.Settings.Default.BlockRegularLoaderClass) as BlockRegularFileLoader;
            }

            domainLoader.Load();
        }

        public void SaveDomainData()
        {
            BlockRegularFileSaver domainSaver = null;
            if (string.IsNullOrEmpty(Properties.Settings.Default.BlockRegularSaverClass))
            {
                domainSaver = new BlockRegularFileSaver();
            }
            else
            {
                domainSaver = Assembly.GetExecutingAssembly().CreateInstance(Properties.Settings.Default.BlockRegularSaverClass) as BlockRegularFileSaver;
            }

            domainSaver.Save();
        }

        public void LoadBlockData()
        {
            BlockCustomFileLoader blockLoader = null;
            if (string.IsNullOrEmpty(Properties.Settings.Default.BlockCustomLoaderClass))
            {
                blockLoader = new BlockCustomFileLoader();
            }
            else
            {
                blockLoader = Assembly.GetExecutingAssembly().CreateInstance(Properties.Settings.Default.BlockCustomLoaderClass) as BlockCustomFileLoader;
            }

            blockLoader.Load();
        }

        public void SaveBlockData()
        {
            BlockCustomFileSaver blockSaver = null;
            if (string.IsNullOrEmpty(Properties.Settings.Default.BlockCustomSaverClass))
            {
                blockSaver = new BlockCustomFileSaver();
            }
            else
            {
                blockSaver = Assembly.GetExecutingAssembly().CreateInstance(Properties.Settings.Default.BlockCustomSaverClass) as BlockCustomFileSaver;
            }

            blockSaver.Save();
        }

        public void LoadProfileData()
        {
            ProfileFileLoader profileLoader = new ProfileFileLoader();
            profileLoader.Load();
        }

        public void SaveProfileData()
        {
            ProfileFileSaver profileSaver = new ProfileFileSaver();
            profileSaver.Save();
        }

        public void LoadTabData()
        {
            TabFileLoader tabLoader = new TabFileLoader();
            tabLoader.Load();
        }

        public void SaveTabData()
        {
            TabFileSaver tabSaver = new TabFileSaver();
            tabSaver.Save();
        }
    }
}
